package com.example.android.services

import android.content.Context
import android.util.Log
import androidx.core.content.edit
import com.example.android.R
import com.example.android.extensions.StringBody
import com.example.android.models.User
import com.example.android.utils.Api
import com.example.android.utils.JsonUtil
import com.example.android.utils.models.Form
import com.example.android.utils.models.MyResult
import com.example.android.viewmodels.SharedViewModel
import java.util.ArrayList

/**
 * @ClassName SharedService
 * @Author Ryan Kamanri
 * @Date 2022/6/7
 * @Description
 **/
class SharedService(
    private val _Context: Context,
    private val _Api: Api,
    private val _JsonUtil: JsonUtil,
    private val _HashMapStorage: HashMap<String, String>) {
    suspend fun GetSelfInfo(model: SharedViewModel): MyResult<User> {
        val token = _HashMapStorage["token"] ?: return MyResult()

        val response = _Api.Get("/user/selectSelfInformation") {
            it.addHeader("jwtToken", token)
        }.StringBody()

        Log.d(toString(), response)

        val result = _JsonUtil.FromJsonObject(response).AsMyResult<Form>()
        when (result.Code) {
            MyResult.OK -> {
                val user = User(
                    result.Data?.get("id") as Int,
                    result.Data?.get("userName") as String?,
                    result.Data?.get("userPassword") as String?,
                    result.Data?.get("name") as String?,
                    result.Data?.get("phone") as String?,
                    result.Data?.get("identity") as String?,
                    result.Data?.get("sex") as String?,
                    result.Data?.get("email") as String?,
                    result.Data?.get("paytype") as String?,
                    result.Data?.get("province") as String?,
                    result.Data?.get("category") as String?,
                    result.Data?.get("address") as String?,
                    result.Data?.get("political") as String?,
                    result.Data?.get("college") as String?,
                    result.Data?.get("major") as String?,
                    result.Data?.get("classes") as String?)
                model.getSelf().value = user
                return MyResult(result.Code, result.Message, user)
                //return MyResult(result.Code, result.Message,InfoList)
            }

            else -> return MyResult()
        }
    }


    fun GetLocalCookies(outCookies: ArrayList<String>): Boolean {
        val sp = _Context.getSharedPreferences(_Context.getString(R.string.context_shared_preferences_name), Context.MODE_PRIVATE)
        val cookiesSet = sp.getStringSet(_Context.getString(R.string.context_cookies_name), null)
            ?: return false

        cookiesSet.forEach {
            outCookies.add(it)
        }
        return true
    }


    fun SetLocalCookies(cookies: ArrayList<String>) {
        val sp = _Context.getSharedPreferences(_Context.getString(R.string.context_shared_preferences_name), Context.MODE_PRIVATE)
        val set = mutableSetOf<String>()
        cookies.forEach {
            set.add(it)
        }
        sp.edit {
            putStringSet(_Context.getString(R.string.context_cookies_name), set)
        }
    }

}